/**
 * 
 */
package leetCode;

/**
 * @author zhong
 *
 */
public class SortArrayByParity {
	public int[] sortArrayByParity(int[] A) {
		int i = 0, j = A.length - 1;
		while (i < j) {
			while (i < A.length && A[i] % 2 == 0) {
				i++;
			}
			while (j >= 0 && A[j] % 2 == 1) {
				j--;
			}
			if (i >= j)
				break;
			swap(A, i, j);
			i++;
			j--;
		}
		return A;
	}

	private void swap(int[] a, int i, int j) {
		int tmp = a[i];
		a[i] = a[j];
		a[j] = tmp;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] A = { 0, 2 };
		A = new SortArrayByParity().sortArrayByParity(A);
		for (int i : A) {
			System.out.println(i);
		}
		System.out.println();

	}

}
